Using Web Service in PowerShell [deprecated]
This is a short example for a PowerShell version 2 .NET web service call.
Before you begin
We created a working example on March 2010 using the following prerequisites:
- PowerShell V2 is available by default with both Windows 2008 R2 and Windows 7 and, via an optional update, previous versions of Windows.
About this task
- Start > Run and type powershell
- In the powershell command window type ise which launches the script editor
- Copy-Paste the script below, save it in something like C:\temp\TrisoftWS-Application-Login.ps1
$applicationName = "ISHCM" $userName = "admin" $password = "admin" $applicationUrl = "http://ish.example.com/ISHWS/application.asmx?WSDL" $applicationWebService = new-webServiceProxy -uri $applicationUrl -UseDefaultCredential $application20Url = "http://ish.example.com/ISHWS/application20.asmx?WSDL" $application20WebService = new-webServiceProxy -uri $application20Url -UseDefaultCredential "GetVersion[" + $applicationWebService.GetVersion() + "]" $context = "" $result = $applicationWebService.Login($applicationName, $userName, $password, [REF]$context) "context[" + $context + "]" $releasedStates = "" $result = $applicationWebService.GetReleasedStates($context, [REF]$releasedStates) "releasedStates[" + @($releasedStates | foreach {$_}) + "]" $currentApplicationName = "" $result = $applicationWebService.GetApplicationName($context, [REF]$currentApplicationName) "currentApplicationName[" + $currentApplicationName + "]" $currentUserName = "" $result = $applicationWebService.GetCurrentUserName($context, [REF]$currentUserName) "currentUserName[" + $currentUserName + "]" $oldPassword = $password $newPassword = -join ($oldPassword[$oldPassword.Length..0]) #reverse a string "admin" --> "nimda" $result = $applicationWebService.ChangePassword($context, $oldPassword, $newPassword) "Changed password from [" + $oldPassword + "] to [" + $newPassword + "] resulted in result[" + $result + "]" $oldPassword = $newPassword $newPassword = $password $result = $applicationWebService.ChangePassword($context, $oldPassword, $newPassword) "Changed password from [" + $oldPassword + "] to [" + $newPassword + "] resulted in result[" + $result + "]" $oldPassword = "a very wrong old password causing an error" $newPassword = $password # in Application10 code the following call results in error 91 $result = $applicationWebService.ChangePassword($context, $oldPassword, $newPassword) "Changed password from [" + $oldPassword + "] to [" + $newPassword + "] resulted in result[" + $result + "]" # in Application20 code the following call results in an thrown exception (as all 2.0 calls do) try { $result = "" $result = $application20WebService.ChangePassword($context, $oldPassword, $newPassword) "Changed password from [" + $oldPassword + "] to [" + $newPassword + "] resulted in result[" + $result + "]" } catch [System.Web.Services.Protocols.SOAPException] { $Error[0].Exception.InnerException.Message }http://ish.example.comwhereishrefers to an example server dedicated to Content Manager. - Press F5 to run it in the ISE editor.
The login function and how to call it
function Application20-Login([string]$applicationName, [string]$userName, [string]$password, [REF]$context)
{
$functionName = "Application20-Login"
Write-Verbose ($functionName + " applicationName[" + $applicationName + "] userName[" + $userName + "] password[" + $password + "] context[" + $context.Value + "]")
$application20Url = $infosharewsurl + [string]"application20.asmx?WSDL"
$application20WebService = new-webServiceProxy -uri $application20Url -UseDefaultCredential
# in Application20 code the following call results in an thrown exception (as all 2.0 calls do)
try
{
$result = $application20WebService.Login($applicationName, $userName, $password, ($context))
}
catch [System.Web.Services.Protocols.SOAPException]
{
($functionName + " catched:")
Return-Result $Error[0].Exception.InnerException.Message
}
Write-Debug ($functionName + " context[" + $context.Value + "]")
}
$infosharewsurl = "http://ish.example.com/ISHWS/"
applicationName = "ISHCM"
$userName = "admin"
$password = "admin"
$context = "NotInitializedYet"
Application20-Login $global:applicationName $global:userName $global:password ([ref]$global:context)
if (($global:context -eq "") -or ($global:context -eq "NotInitializedYet"))
{
Write-Error ("Login for " + $userName + " on a " + (Application20-GetVersion) + " system failed!!")
break
}